package com.bjpower.springboot.util;


import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Author: yg
 * @create 2025-01-23 - 22:38
 * @
 */
public class JDBCUtils {
    /*
     * @Author YG
     * @Description //创建连接连接数据库操作
     * @Title getConnection
     * @return Connection conn 数据库连接实例
     **/
    public static Connection getConnection() throws Exception {
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties pros = new Properties();
        pros.load(is);
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driver = pros.getProperty("driver");
        Class.forName(driver);
        //4.预编译sql语句，返回Prepare实例
        return DriverManager.getConnection(url, user, password);
    }

    /*
     * @Author YG
     * @Description //关闭连接操作
     * @Title closeResource
     * @Param conn【数据库连接对象】 ppst【操作数据库类】
     **/
    public static void closeResource(Connection conn, Statement ppst){
        try {
            if(conn != null){
                conn.close();
            }
        }catch (Exception e){e.printStackTrace();}
        try {
            if(ppst != null){
                ppst.close();
            }
        }catch (Exception e){e.printStackTrace();}
    }
    public static void closeResource(Connection conn, Statement ppst, ResultSet resultSet){
        JDBCUtils.closeResource(conn, ppst);
        try {
            if(resultSet != null){
                resultSet.close();
            }
        }catch (Exception e){e.printStackTrace();}
    }
}
